package com.student.dms;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AppDatabase;
import com.student.db.RoomDao;
import com.student.db.RoomEntity;
import com.student.db.ScoreDao;
import com.student.db.ScoreEntity;
import com.student.dms.databinding.ActivityDayScoreBinding;
import com.student.dms.databinding.ActivityEditDayScoreBinding;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ActivityEditDayScore extends BaseActivity {

    private AppDatabase roomDatabase;
    private ActivityEditDayScoreBinding activityEditDayScoreBinding;
    private RoomEntity randomRoomEntity;
    private List<RoomEntity> allRoomEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roomDatabase = DMSApplication.getApplication().getRoomDatabase();
        RoomDao roomDao = roomDatabase.provideRoomDao();
        ScoreDao scoreDao = roomDatabase.provideScoreDao();
        activityEditDayScoreBinding = ActivityEditDayScoreBinding.inflate(getLayoutInflater());
        setContentView(activityEditDayScoreBinding.getRoot());
        activityEditDayScoreBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });

        activityEditDayScoreBinding.randomRoom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                disposable.add(roomDao.getRooms().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(list -> {
                    Random random = new Random();
                    //随机抽查一个寝室
                    int randomIndex = random.nextInt(list.size());
                    randomRoomEntity = list.get(randomIndex);
                    allRoomEntity=list;
                    activityEditDayScoreBinding.editRoomNo.setText(randomRoomEntity.getRoomNo());
                }));
            }
        });

        activityEditDayScoreBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String editRoomNo = activityEditDayScoreBinding.editRoomNo.getText().toString().trim();
                String eidtBedScore = activityEditDayScoreBinding.eidtBedScore.getText().toString().trim();
                String editFloorScore = activityEditDayScoreBinding.editFloorScore.getText().toString().trim();
                String editDesktopScore = activityEditDayScoreBinding.editDesktopScore.getText().toString().trim();
                String editBalconyScore = activityEditDayScoreBinding.editBalconyScore.getText().toString().trim();
                if (checkInputIsNull(editRoomNo)||checkInputIsNull(eidtBedScore) || checkInputIsNull(editFloorScore)||checkInputIsNull(editDesktopScore) || checkInputIsNull(editBalconyScore)) {
                    ToastUtils.showToast(ActivityEditDayScore.this, "评分输入不能为空！");
                    return;
                }
                if(allRoomEntity!=null){
                    List<RoomEntity> collect = allRoomEntity.stream().filter(roomEntity -> {
                        return TextUtils.equals(roomEntity.getRoomNo(), editRoomNo);
                    }).collect(Collectors.toList());
                    if(collect==null || collect.size()<1){
                        ToastUtils.showToast(ActivityEditDayScore.this, "宿舍列表为空！");
                    }
                }
                //评分插入
                ScoreEntity scoreEntity = new ScoreEntity();
                scoreEntity.setRoomNo(editRoomNo);
                scoreEntity.setBedScore(eidtBedScore);
                scoreEntity.setFloorScore(editFloorScore);
                scoreEntity.setDesktopScore(editDesktopScore);
                scoreEntity.setBalconyScore(editBalconyScore);

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String time = simpleDateFormat.format(new Date());
                scoreEntity.setScoreTime(time);

                disposable.add(scoreDao.insert(scoreEntity).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
                        ()->{
                            ToastUtils.showToast(ActivityEditDayScore.this, "评分成功！");
                            ActivityEditDayScore.this.finish();
                        }
                ));
            }
        });
    }



    private boolean checkInputIsNull(String editText) {
        return TextUtils.isEmpty(editText);
    }
}
